package com.rd.p2p.app;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.qdlc.p2p.common.constant.BorrowStyle;
import org.apache.struts2.convention.annotation.Action;

import com.alibaba.fastjson.JSON;
import com.qdlc.p2p.biz.web.action.BaseAction;
import com.qdlc.p2p.common.constant.BorrowType;
import com.qdlc.p2p.common.constant.Global;
import com.qdlc.p2p.common.exception.BusinessException;
import com.qdlc.p2p.common.util.BigDecimalUtil;
import com.qdlc.p2p.common.util.DateUtil;
import com.qdlc.p2p.common.util.NumberUtil;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.model.interest.EachPlan;
import com.qdlc.p2p.dal.model.interest.InstallmentRepaymentCalculator;
import com.qdlc.p2p.dal.model.interest.InterestCalculator;
import com.qdlc.p2p.dal.model.interest.MonthlyInterestCalculator;
import com.qdlc.p2p.dal.model.interest.OnetimeRepaymentCalculator;

@SuppressWarnings("rawtypes")
public class AppPublicAction extends BaseAction {
	
	
	
	/**
	 * 返回系通处理的信息
	 * @return
	 * @throws Exception
	 */
	@Action("/app/public/getResult")
	public String getTenderResult() throws Exception{
		String result = "";
		String resultFlag = paramString("resultFlag");
		if(StringUtil.isBlank(resultFlag)){
			result="查询处理信息错误";
		}else{
			result = (String)Global.TASK_RESULT_MAP.get(resultFlag);
			Global.TASK_RESULT_MAP.remove(resultFlag);//系统消息取出来之后立即删除，保证集合为空
		}
		Map<String,String> map = new HashMap<String, String>();
		map.put("msg_data", result);
		printWebJson(JSON.toJSONString(map));
		return null;
	}
	
	/**
	 * 异步校验加息券所产生的利息
	 * @throws IOException 
	 */
	@Action("/app/public/upRateInterestJson")
	public void  upRateInterestJson() throws IOException{
		Map<String, Object> data = new HashMap<String, Object>();
		try {
			double upapr = paramDouble("upapr"); //加息幅度
			double apr = paramDouble("apr");  //原始利率
			int limit = paramInt("limit");  //借款期限
			double money = paramDouble("money"); //金额
			int style = paramInt("style"); //还款方式
			int isDay = paramInt("isDay");
			InterestCalculator ic = doUpRateInterestCalculator(style, money, apr/100, isDay, limit); //先计算出原始应还利率
			double newapr = BigDecimalUtil.add(upapr+apr)/100;
			InterestCalculator newic = doUpRateInterestCalculator(style, money, newapr, isDay, limit); //先计算出原始应还利率
			double oldInterestTotal = BigDecimalUtil.sub(ic.repayTotal(), money);  //原始利息总和
			double upInterestTotal = BigDecimalUtil.sub(newic.repayTotal(),money);  //加息后利息总和
			//计算利息差值
			double subInterest = BigDecimalUtil.sub(upInterestTotal,oldInterestTotal);
			data.put("result", true);
			data.put("upInterest", NumberUtil.format2(subInterest));
		} catch (Exception e) {
			data.put("result", false);
			data.put("upInterest", 0);
		}
		printJson(JSON.toJSON(data));
		
	}
	
	
	
	
	/**
	 * @param style 还款方式
	 * @param money 投标金额
	 * @param apr  利率
	 * @param isDay 是否天标
	 * @param timeLimit 借款期限
	 * @return  根据还款方式，返回不同的利息计算器
	 */
	private InterestCalculator doUpRateInterestCalculator(int style,double money ,double apr,int isDay,int timeLimit){
		InterestCalculator ic = null;
		Date date = DateUtil.getNow();
		List<EachPlan> list = new ArrayList<EachPlan>();
		if (isDay == 1 && style == BorrowStyle.STYLE_ONETIME_REPAYMENT) { // 天标,一次性还款
			ic = new OnetimeRepaymentCalculator(money, apr, date, 1, 0);
		} else if (style == BorrowStyle.STYLE_ONETIME_REPAYMENT) { // 一次性还款
			ic = new OnetimeRepaymentCalculator(money, apr, date, timeLimit, 0);
		} else if (style == BorrowStyle.STYLE_MONTHLY_INTEREST) { // 每月还息到期还本
			ic = new MonthlyInterestCalculator(money, apr, date, timeLimit, true, 0);
		} else if (style == BorrowStyle.STYLE_INSTALLMENT_REPAYMENT) { // 等额本息
			ic = new InstallmentRepaymentCalculator(money, apr, date, timeLimit, 0);
		}
		if (isDay == 1) {
			list = ic.calculator(timeLimit);
		} else {
			list = ic.calculator();
		}
		// 如果利息小于0则提示用户投标金额过小
		for (EachPlan eachPlan : list) {
			if (eachPlan.getInterest() < 0.01) {
				throw new BusinessException("投资金额过小");
			}
		}
		return ic;
	}
	

}
